package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.PaasMenuDao;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.domain.entity.ext.PaasMenuEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * MenuModel业务实现
 * @author wufeng
 * @time 2018-8-8 10:10
 */
@Service("paasMenuModel")
public class PaasMenuModelImpl implements PaasMenuModel {

    private static final String COLUMNS = " id, corpid, app_id, parent_id, name, name_en, alias, sort, type, url, mobile_url, saas_mark, enable, color, icon, creator_id, add_time, update_time, del, distributor_mark";
    @Resource
    private PaasMenuDao paasMenuDao;

    @Override
    public Integer insert(PaasMenuEntity entity) {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return paasMenuDao.insert(entity);
    }

    @Override
    public List<PaasMenuEntity> insertBatch(List<PaasMenuEntity> list) {
        paasMenuDao.insertBatch(list);
        return list;
    }

    @Override
    public List<PaasMenuEntity> updateBatch(List<PaasMenuEntity> list, String corpid) {
        paasMenuDao.updateBatch(list, corpid);
        return list;
    }

    @Override
    public List<PaasMenuEntity> list(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        return paasMenuDao.findEntitys(map);
    }

    @Override
    public List<PaasMenuEntity> findEntitys(Map<String, Object> params) {
        return paasMenuDao.findEntitys(params);
    }

    @Override
    public PaasMenuEntity getByKey(Long key, String corpid) {
        return paasMenuDao.getByKey(key, corpid);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {

        return paasMenuDao.getEntitysCount(map);
    }

    @Override
    public Integer delete(Long key, String corpid) {
        return paasMenuDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer update(PaasMenuEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return paasMenuDao.update(entity);
    }

    @Override
    public void save(PaasMenuEntity entity) {
        if (entity.getId() == null || entity.getId() == 0) {
            insert(entity);
        } else {
            update(entity);
        }
    }

    @Override
    public Integer updateSort(List<PaasMenuEntity> entity, String corpid) {
        return paasMenuDao.updateSort(entity, corpid);
    }

    @Override
    public List<PaasMenuEntityExt> getMenuList(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        return paasMenuDao.getMenuList(map);
    }

    @Override
    public List<PaasMenuEntityExt> getAppProcessFormList(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        return paasMenuDao.getAppProcessFormList(map);
    }

    @Override
    public List<PaasMenuEntityExt> getSomeAppProcessFormList(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        return paasMenuDao.getSomeAppProcessFormList(map);
    }

    @Override
    public List<PaasMenuEntityExt> getAppFormList(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        return paasMenuDao.getAppFormList(map);
    }

    @Override
    public List<PaasMenuEntityExt> formList(Map<String, Object> map) {
        map.put("columns", COLUMNS);
        return paasMenuDao.formList(map);
    }

    @Override
    public PaasMenuEntity getByAlias(String alias, Long appId, String corpid) {
        return paasMenuDao.getByAlias(alias, appId, corpid);
    }

    @Override
    public PaasMenuEntity getByAlias(String alias, String corpid) {
        return paasMenuDao.getByAlias(alias, null, corpid);
    }

    @Override
    public PaasMenuEntity getByAlias4Distributor(String alias, String corpid, Integer distributorMark) {
        return paasMenuDao.getByAlias4Distributor(alias, corpid, distributorMark);
    }

    @Override
    public List<PaasMenuEntity> getList(String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        //查询
        return findEntitys(param);
    }

    @Override
    public Map<String, String> getIdNameMap(String corpid) {
        List<PaasMenuEntity> list = getList(corpid);
        //菜单id-菜单名称
        Map<String, String> idNameMap = new HashMap<>(list.size());
        for (PaasMenuEntity entity : list) {
            idNameMap.put(entity.getId().toString(), entity.getName());
        }
        return idNameMap;
    }

    @Override
    public Integer updateBatchTemp(List<PaasMenuEntity> list) throws XbbException {
        return paasMenuDao.updateBatchTemp(list);
    }

    @Override
    public void batchUpdateDel(Integer del, Integer updateTime, List<Long> idIn, String corpid) {
         paasMenuDao.batchUpdateDel(del,updateTime,idIn,corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> idIn, String corpid) {
        return paasMenuDao.deleteBatch(idIn, corpid);
    }

    @Override
    public Integer maxSortInSubMenu(String corpid, Long parentId, Integer del) {
        return paasMenuDao.maxSortInSubMenu(corpid, parentId, del);
    }

    @Override
    public Integer deleteByAlias(List<String> aliasList, String corpid) {
        if (CollectionsUtil.isEmpty(aliasList) || StringUtil.isEmpty(corpid)) {
            return 0;
        }
        return paasMenuDao.deleteByAlias(aliasList, corpid);
    }

    @Override
    public Map<Integer, String> getMenuNameByAlias(List<String> aliasIn, String corpid, Integer businessType) {
        // 记录businessType与menuName的关系
        Map<Integer, String> paasBusinessTypeToName = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionUtils.isEmpty(aliasIn)) {
            return paasBusinessTypeToName;
        }
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put("aliasIn", aliasIn);
        param.put("del", DelEnum.NORMAL.getDel());
        // 经销商拆分菜单的需要经销商的菜单关联新建（经销商联系人等） + 非经销商的菜单关联新建（回款等）
        boolean isDistributor = XbbRefTypeEnum.isDistributorBusinessType(businessType);
        if (!isDistributor) {
            param.put("distributorMark", DistributorMarkEnum.OTHER.getCode());
        }
        List<PaasMenuEntity> paasMenuEntities = findEntitys(param);

        Map<Integer, PaasMenuEntity> menuMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (isDistributor) {
            paasMenuEntities.forEach(item -> {
                int code = XbbRefTypeEnum.getByAlias(item.getAlias()).getCode();
                if (!menuMap.containsKey(code) || Objects.equals(item.getDistributorMark(), DistributorMarkEnum.DISTRIBUTOR.getCode())) {
                    menuMap.put(code, item);
                }
            });
            menuMap.forEach((key, value) -> paasBusinessTypeToName.put(key, value.getName()));
        } else {
            paasMenuEntities.forEach(item-> paasBusinessTypeToName.put(XbbRefTypeEnum.getByAlias(item.getAlias()).getCode(),item.getName()));
        }
        return paasBusinessTypeToName;
    }

    @Override
    public boolean enableMenu(String corpid, String alias) throws XbbException {
        if (StringUtil.isEmpty(alias)) {
            return false;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", BasicConstant.ZERO);
        param.put("alias", alias);
        param.put("saasMark", SaasMarkEnum.SAAS.getCode());
        param.put("enable", BasicConstant.ONE);
        Integer count = getEntitysCount(param);
        return count > 0;
    }
}
